package com.xk.demo.user.service.impl;

import com.xk.demo.login.entity.MenuLevelEntity;
import com.xk.demo.mybatis.entity.PtMenuEntity;
import com.xk.demo.mybatis.entity.PtUserEntity;
import com.xk.demo.mybatis.flexigrid.PadingRstType;
import com.xk.demo.mybatis.flexigrid.PagingBean;
import com.xk.demo.mybatis.ztree.ZtreeEntity;
import com.xk.demo.spring.security.GdmtUserEntity;
import com.xk.demo.user.entity.UserSearchEntity;
import com.xk.demo.user.respository.UserDao;
import com.xk.demo.user.service.UserService;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class UserServiceImpl implements UserService {
    private final static Integer MENU_FIRST_LEVEL=1;
    @Resource
    private UserDao userDao;

    @Override
    public PtUserEntity queryUserInfoByUserName(String username) {
        return userDao.queryUserInfoByUserName(username);
    }

    @Override
    public List<MenuLevelEntity> queryMenuInfoByUserLang(String lang) {
        GdmtUserEntity gdmtUserEntity =(GdmtUserEntity) SecurityContextHolder.getContext()//令牌的方式
                .getAuthentication()
                .getPrincipal();


        List<MenuLevelEntity> levelEntities=new ArrayList<>();
        List<PtMenuEntity> ptMenuEntities=userDao.queryMenuInfoByUserLang(gdmtUserEntity.getUserUuid(),lang);
        MenuLevelEntity menuLevelEntity1=null;
        MenuLevelEntity menuLevelEntity2=null;
        MenuLevelEntity menuLevelEntity3=null;
        for(PtMenuEntity ptMenuEntity1:ptMenuEntities){
            if(MENU_FIRST_LEVEL.equals(ptMenuEntity1.getMenuLevel())){
                menuLevelEntity1=new MenuLevelEntity(ptMenuEntity1);
                levelEntities.add(menuLevelEntity1);
                for(PtMenuEntity ptMenuEntity2:ptMenuEntities){
                    if(ptMenuEntity1.getMenuId().equals(ptMenuEntity2.getParentId())){
                        menuLevelEntity2=new MenuLevelEntity(ptMenuEntity2);
                        menuLevelEntity1.addList(menuLevelEntity2);
                        for(PtMenuEntity ptMenuEntity3:ptMenuEntities){
                            if(ptMenuEntity2.getMenuId().equals(ptMenuEntity3.getParentId())){
                                menuLevelEntity3=new MenuLevelEntity(ptMenuEntity3);
                                menuLevelEntity2.addList(menuLevelEntity3);
                            }
                        }
                    }

                }
            }

        }


        return levelEntities;
    }

    @Override
    public void addUser(PtUserEntity user) {
        String uuid = UUID.randomUUID().toString();
        user.setUserUuid(uuid);
        Date date=new Date();
        Timestamp nousedate = new Timestamp(date.getTime());
        user.setDelFlag(0);
        userDao.addUser(user,nousedate);
    }

    @Override
    public void updateUserByUserId(PtUserEntity user) {
        Date date=new Date();
        Timestamp nousedate = new Timestamp(date.getTime());
        userDao.updateUserByUserId(user,nousedate);
    }

    @Override
    public List<String> queryMenuInfoList(String userUuid) {
        Locale locale= LocaleContextHolder.getLocale();

        return userDao.queryMenuInfoList(userUuid);
    }

    @Override
    public PadingRstType<PtUserEntity> getUserInfolistByPage(UserSearchEntity search, PagingBean paging) {
        paging.deal(PtUserEntity.class);
        PadingRstType<PtUserEntity> padingRstType = new PadingRstType<>();
        padingRstType.setPage(paging.getPage());
        List<PtUserEntity> list=userDao.queryUserListByPading(search,paging);
        Integer total = userDao.queryUserListTotal(search);
        padingRstType.setRawRecords(list);
        padingRstType.setTotal(total);
        padingRstType.putItems();
        return padingRstType;
    }

    @Override
    public void deleteUserById(String userUuid) {
        userDao.deleteUserById(userUuid);
    }

    @Resource
    private MessageSource messageSource;

    @Override
    public List<ZtreeEntity> getRoleZtreeListByUserId(String userUuid) {

        Locale locale= LocaleContextHolder.getLocale();
        String msgStr=messageSource.getMessage("user.role.info",null,locale);
        List<ZtreeEntity> list= userDao.getRoleZtreeList();

        List<String> roleUUidList=userDao.queryRoleRefUserByUserId(userUuid);//之前的角色状态
        for (ZtreeEntity item:list){
            for(String roleUuid:roleUUidList){
                if(roleUuid.equals(item.getId())){
                    item.setChecked(true);
                }
            }
        }

        ZtreeEntity root=new ZtreeEntity();
        root.setId("0");
        root.setpId("-1");
        root.setName(msgStr);
        list.add(root);
        return list;
    }

    @Override
    public void saveRoleByUserId(String userUuid, String roleUuids) {
        String[] roleUuidArray=roleUuids.split(",");
        userDao.deleteUserRefRole(userUuid);
        for(String roleUuid:roleUuidArray){
            userDao.insertUserRefRole(userUuid,roleUuid);//插入记录
        }
    }


}
